package com.heyqing.medium.day1;

import java.util.HashSet;
import java.util.Set;

/**
 * ClassName:LengthOfLongestSubstring
 * Package:com.heyqing.medium.day1
 * Description:
 * 0003
 *
 * @Date:2024/10/2
 * @Author:Heyqing
 */
public class LengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        int max = 0, p = 0, count = 0, index = 0;
        Set<Character> set = new HashSet<>();
        while (p < s.length()) {
            if (s.length() - p <= max) return max;
            if (index == s.length()) return Math.max(s.length() - p, max);
            if (set.add(s.charAt(index))) {
                index++;
                count++;
            } else {
                max = Math.max(max, count);
                p++;
                index = p;
                count = 0;
                set.clear();
            }
        }
        return max;
    }
}
